#include "MainGame.h"
#include "../Game.h"
#include "../../Renderer/Utility/TextureManager.h"
#include "../Definitions.h"
#include "../../Input/Input.h"
#include "../../Renderer/D3D9Renderer.h"
#include "../../Utility/Misc/SLList.h"
#include "../../Utility/Misc/Graph.h"
#include <sstream>

MainGame::MainGame()
{

}

MainGame::~MainGame()
{

}

MainGame* MainGame::GetInstance()
{
	static MainGame instance;
	return &instance;
}

void MainGame::Initialize()
{
	EventSystem::RegisterClient(EVENT_INPUT, HandleKeyInput);
	numberOfLettersOnBoard = 0;
	doubleClickTimer = 1.5f;

	int tempImage;
	string path;
	
	string bmp = ".bmp";
	char number;
	for(int i = 48; i < 58; ++i)
	{
		path = "Res/Textures/";
		number = i;
		path += number;
		path += bmp;
		tempImage = TextureManager::LoadTexture(path);
		numberImages.push_back(tempImage);
	}

	char letter;
	string jpg = ".jpg";
	for(int i = 64; i < 91; ++i)
	{
		path = "Res/Textures/";
		letter = i;
		path += letter;
		path += jpg;
		tempImage = TextureManager::LoadTexture(path);
		letterImages.push_back(tempImage);
	}
	selectedImage = TextureManager::LoadTexture("Res/Textures/Selected.bmp");
	boardImage = TextureManager::LoadTexture("Res/Textures/Board.jpg");
	nextTileImage = TextureManager::LoadTexture("Res/Textures/NextTile.bmp");
	gameOverImage = TextureManager::LoadTexture("Res/Textures/GameOver.jpg");
	doubleClickImage = TextureManager::LoadTexture("Res/Textures/DoubleClick.bmp");

	InitializeGrabBag();

	int size = lettersGrabBag.size();

	for(int i = 0; i < GameInfo::BoardSize; ++i)
	{
		board.append('/');
		boardGrid.addVertex(board[i]);
	}

	int grabIndex;
	int column;
	for(int i = 0; i < 10; ++i)
	{
		grabIndex = rand() % (98 - numberOfLettersOnBoard);
		column = rand() % 8;
		AddLetterToColumn(lettersGrabBag[grabIndex], column);

		char temp = lettersGrabBag[lettersGrabBag.size() - 1];
		lettersGrabBag[lettersGrabBag.size() - 1] = lettersGrabBag[grabIndex];
		lettersGrabBag[grabIndex] = temp;
		lettersGrabBag.pop_back();
		numberOfLettersOnBoard ++;
	}

	for(int i = 0; i < 3; ++i)
	{
		grabIndex = rand() % (98 - numberOfLettersOnBoard);
		upcomingLetters[i] = (lettersGrabBag[grabIndex]);
	}

#pragma region SETTING UP THE GRID

for(int i = 0; i < GameInfo::Rows; ++i)
{
	for(int j = 0; j < GameInfo::Columns; ++j)
	{
		int arrIndex = i * GameInfo::Columns + j;

		int switchValue = arrIndex % GameInfo::Columns;

		//TOP ROW
		if(i == 0)
		{
			//FIRST COLUMN
			if(j == 0)
			{
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns + 1);
			}
			//LAST COLUMN
			else if(j == (GameInfo::Columns - 1))
			{
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
			}
			//MIDDLE COLUMNS
			else
			{
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns + 1);
			}
		}
		//BOTTOM ROW
		else if(i == (GameInfo::Rows - 1))
		{
			//FIRST COLUMN
			if(j == 0)
			{
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns + 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
			}
			//LAST COLUMN
			else if(j == (GameInfo::Columns - 1))
			{
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
			}
			//MIDDLE COLUMNS
			else
			{
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns + 1);
			}
		}
		//MIDDLE ROWS
		else
		{
			//FIRST COLUMN
			if(j == 0)
			{
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns + 1);
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns + 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
			}
			//LAST COLUMN
			else if(j == (GameInfo::Columns - 1))
			{
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
			}
			//MIDDLE COLUMNS
			else
			{
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex - GameInfo::Columns + 1);
				boardGrid[arrIndex].addEdge(arrIndex - 1);
				boardGrid[arrIndex].addEdge(arrIndex + 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns - 1);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns);
				boardGrid[arrIndex].addEdge(arrIndex + GameInfo::Columns + 1);
			}
		}
	}
}

#pragma endregion

}

void MainGame::StartNewGame()
{
	board.clear();

	for(int i = 0; i < GameInfo::BoardSize; ++i)
	{
		board.append('/');
	}

	numberOfLettersOnBoard = 0;
	lettersGrabBag.clear();
	InitializeGrabBag();
	int grabIndex;
	int column;
	for(int i = 0; i < 10; ++i)
	{
		grabIndex = rand() % (98 - numberOfLettersOnBoard);
		column = rand() % 8;
		AddLetterToColumn(lettersGrabBag[grabIndex], column);

		char temp = lettersGrabBag[lettersGrabBag.size() - 1];
		lettersGrabBag[lettersGrabBag.size() - 1] = lettersGrabBag[grabIndex];
		lettersGrabBag[grabIndex] = temp;
		lettersGrabBag.pop_back();
		numberOfLettersOnBoard ++;
	}

	for(int i = 0; i < 3; ++i)
	{
		grabIndex = rand() % (98 - numberOfLettersOnBoard);
		upcomingLetters[i] = (lettersGrabBag[grabIndex]);
	}
}

void MainGame::Update(float _dt)
{
	int selectedIndex;
	if(!Game::gameOver)
	{
		//LEFT CLICK
		if(Input::MouseButtonReleased(0))
		{
			bool isALetter = ClickedOnLetter(selectedIndex);

			if(isALetter && doubleClickTimer < .2f)
			{
				selectedLetters.clear();
				SelectLetter(true, selectedIndex);
			}
			else// if(isALetter && doubleClickTimer > .2f)
			{
				if(renderDoubleClick)
				{
					SwapLetters();
				}
				else
				{
					SelectLetter(false, selectedIndex);
				}
			}
			doubleClickTimer = 0;
		}
		//RIGHT CLICK
		if(Input::MouseButtonReleased(1))
		{
			//SELECT LETTER IF IT IS ACTUALLY A LETTER AND IT IS NOT ALREADY SELECTED
			selectedLetters.clear();
		}

		if(lettersDropped >= 10)
		{
			//ADD ROW OF ROCKS
			for(int i = 0; i < GameInfo::Columns; ++i)
			{
				//CAN ADD TO COLUMN == CONTINUE PLAYING
				if(AddLetterToColumn('@', i)){}
				//GAMEOVER
				else
				{
					Game::gameOver = true;
				}
			}
			lettersDropped = 0;
		}
		doubleClickTimer += _dt;
	}
	else
	{
		if(Input::MouseButtonReleased(0))
		{
			//START NEW GAME
			StartNewGame();
			Game::gameOver = false;
			score = 0;
			doubleClickTimer = 1.5f;
		}
	}
}

void MainGame::Render()
{
	if(!Game::gameOver)
	{
		TextureManager::DrawTexture(boardImage, 128, 272, 1.0f, 1.0f, NULL, 0, 0, 0);
		TextureManager::DrawTexture(nextTileImage, 204, 16, 1.0f, 1.0f, NULL, 0, 0, 0);

		stringstream ss;
		ss << score;
		string numberString = ss.str();
		int posX = 16;
		for(unsigned int i = 0; i < numberString.size(); ++i)
		{
			int number = numberString[i] - 48;
			TextureManager::DrawTexture(numberImages[number], posX, 16, 1.0f, 1.0f, NULL, 0, 0, 0);
			posX += 16;
		}

		//TODO: WHEN BOARD SIZE CHANGES
		//RENDERING THE CURRENT BOARD
		for(int i = 0; i < GameInfo::BoardSize; ++i)
		{
			if(board[i] != '/')
			{
				int posX = ((i % GameInfo::Columns) * 32) + 16;
				int posY = 400 - (((8 - (i / GameInfo::Rows)) * 32) - 16);

				int img = (board[i] - 64);
				TextureManager::DrawTexture(letterImages[img], posX, posY, 1.0f, 1.0f, NULL, 0, 0, 0);
			}
		}

		//TODO: WHEN BOARD SIZE CHANGES
		//RENDERING THE SELECTED LETTERS
		int sizeOfSelectedLetters = selectedLetters.size();
		for(int i = 0; i < sizeOfSelectedLetters; ++i)
		{
			int posX = ((selectedLetters[i].arrIndex % GameInfo::Columns) * 32) + 16;
			int posY = 400 - (((8 - (selectedLetters[i].arrIndex / GameInfo::Rows)) * 32) - 16);

			TextureManager::DrawTexture(selectedImage, posX, posY, 1.0f, 1.0f, NULL, 0, 0, 0);
		}

		//TODO: WHEN BOARD SIZE CHANGES
		//RENDERING THE SWAP LETTER WITH YELLOW
		if(renderDoubleClick)
		{
			int posX = ((swapLetter.arrIndex % GameInfo::Columns) * 32) + 16;
			int posY = 400 - (((8 - (swapLetter.arrIndex / GameInfo::Rows)) * 32) - 16);
			TextureManager::DrawTexture(doubleClickImage, posX, posY, 1.0f, 1.0f, NULL, 0, 0, 0);
		}

		//RENDER THE WORD AS IT IS CREATED
		for(int i = 0; i < sizeOfSelectedLetters; ++i)
		{
			int posX = 16 + (32 * i);
			int img = (selectedLetters[i].letter - 64);
			TextureManager::DrawTexture(letterImages[img], posX, 128, 1.0f, 1.0f, NULL, 0, 0, 0);
		}

		//RENDER UPCOMING LETTERS
		for(int i = 0; i < 3; ++i)
		{
			int posX = 284;
			int posY = 16 + (32 * i);

			int img = (upcomingLetters[i] - 64);
			TextureManager::DrawTexture(letterImages[img], posX, posY, 1.0f, 1.0f, NULL, 0, 0, 0);
		}
	}
	//GAMEOVER
	else
	{
		TextureManager::DrawTexture(gameOverImage, 150, 200, 1.0f, 1.0f, NULL, 0, 0, 0);

		stringstream ss;
		ss << score;
		string numberString = ss.str();
		int posX = 16;
		for(unsigned int i = 0; i < numberString.size(); ++i)
		{
			int number = numberString[i] - 48;
			TextureManager::DrawTexture(numberImages[number], posX, 16, 1.0f, 1.0f, NULL, 0, 0, 0);
			posX += 16;
		}
	}
}

void MainGame::Shutdown()
{
	EventSystem::UnregisterClient(EVENT_INPUT, HandleKeyInput);
}

void MainGame::CheckBoard()
{
// 	int startPos = 0;
// 	while(startPos < 64)
// 	{
// 		if(boardGrid.FindFirstLetter())
// 	}
}

bool MainGame::ClickedOnLetter(int& _selectedIndex)
{
	POINT cursorPos;
	int col = 0;
	int row = 0;

	GetCursorPos(&cursorPos);
	ScreenToClient(D3D9Renderer::hwnd, &cursorPos);

	//TODO: WHEN BOARD SIZE CHANGES
	if((cursorPos.x > 0 && cursorPos.x < GameInfo::Columns * 32) &&
		(cursorPos.y < 400 && cursorPos.y > (400 - (GameInfo::Rows * 32))))
	{
		col = (cursorPos.x / 32);
		cursorPos.y -= (400 - (GameInfo::Rows * 32));
		row = (cursorPos.y / 32);
	}
	else
	{
		_selectedIndex = -1;
		return false;
	}

	_selectedIndex = (row * GameInfo::Columns) + col;

	if(board[_selectedIndex] != '/')
		return true;


	return false;
}

void MainGame::SelectLetter(bool _doubleClick, int _selectedIndex)
{
	renderDoubleClick = false;
	int col = _selectedIndex % GameInfo::Columns;

	if(!_doubleClick)
	{
		int sizeOfSelectedLetters = selectedLetters.size();
		bool cantSelect = false;
		for(int i = 0; i < sizeOfSelectedLetters; ++i)
		{
			if(selectedLetters[i].arrIndex == _selectedIndex)
			{
				if(i == sizeOfSelectedLetters - 1)
				{
					selectedLetters.pop_back();
					return;
				}
				else
				{
					cantSelect = true;
					break;
				}

			}
		}

		if(((!cantSelect && sizeOfSelectedLetters == 0) ||
			(!cantSelect && boardGrid.IsAnEdge(_selectedIndex, &boardGrid, selectedLetters[sizeOfSelectedLetters - 1].arrIndex))))
		{
			if(board[_selectedIndex] != '/')
			{
				SelectedLetter tempLetter;
				tempLetter.arrIndex = _selectedIndex;
				tempLetter.letter = board[_selectedIndex];
				selectedLetters.push_back(tempLetter);
			}
			else if(board[_selectedIndex] == '/' && sizeOfSelectedLetters == 0)
			{
				//DROPPING THE NEXT LETTER IN THE QUEUE
				if(AddLetterToColumn(upcomingLetters[0], col))
				{
					char b = upcomingLetters[1];
					char c = upcomingLetters[2];

					upcomingLetters[0] = b;
					upcomingLetters[1] = c;

					int grabIndex = rand() % (110 - numberOfLettersOnBoard);
					if(grabIndex < (98 - numberOfLettersOnBoard))
					{
						upcomingLetters[2] = lettersGrabBag[grabIndex];
					}
					else
						upcomingLetters[2] = '@';

					lettersDropped ++;
				}
			}
		}
	}
	else
	{
		if(board[_selectedIndex] != '/')
		{
			swapLetter.arrIndex = _selectedIndex;
			swapLetter.letter = board[_selectedIndex];
			renderDoubleClick = true;
		}
	}
}

void MainGame::SwapLetters()
{
	POINT cursorPos;
	int col = 0;
	int row = 0;

	GetCursorPos(&cursorPos);
	ScreenToClient(D3D9Renderer::hwnd, &cursorPos);

	//TODO: WHEN BOARD SIZE CHANGES
	if((cursorPos.x > 0 && cursorPos.x < GameInfo::Columns * 32) &&
		(cursorPos.y < 400 && cursorPos.y > (400 - (GameInfo::Rows * 32))))
	{
		col = (cursorPos.x / 32);
		cursorPos.y -= (400 - (GameInfo::Rows * 32));
		row = (cursorPos.y / 32);
	}
	else
		return;

	int selectedIndex = (row * GameInfo::Columns) + col;

	if(board[selectedIndex] != '/')
	{
		char tempChar = board[selectedIndex];
		board[selectedIndex] = swapLetter.letter;
		board[swapLetter.arrIndex] = tempChar;

		renderDoubleClick = false;
	}
	
}

void MainGame::CheckSelectedWord()
{
	int sizeOfSelectedLetters = selectedLetters.size();
	string word = "";

	for(int i = 0; i < sizeOfSelectedLetters; ++i)
	{
		word += tolower(selectedLetters[i].letter);
	}

	if(Game::dictionary.find(word) != -1)
	{
		RemoveLetters();
		
		for(int i = 0; i < sizeOfSelectedLetters; ++i)
		{
			//ADD LETTERS BACK TO THE GRAB BAG
			lettersGrabBag.push_back(selectedLetters[i].letter);

			//ADD ONTO THE SCORE
			score += GetValueOfLetter(selectedLetters[i].letter);
		}

		for(unsigned int i = 0 ; i < board.size(); ++i)
		{
			if(board[i] == '@')
			{
				for(int j = 0; j < sizeOfSelectedLetters; ++j)
				{
					if(boardGrid.IsAnEdge(selectedLetters[j].arrIndex, &boardGrid, i))
					{
						//TURN THE ROCK INTO A LETTER
						int grabIndex = rand() % (98 - numberOfLettersOnBoard);
						board[i] = lettersGrabBag[grabIndex];
					}
				}				
			}
		}

		numberOfLettersOnBoard += sizeOfSelectedLetters;

		selectedLetters.clear();
	}
}

void MainGame::RemoveLetters()
{	
	//SORT THE LIST OF LETTERS SO THAT THE ONES AT THE TOP ARE FIRST	
	int sizeOfSelectedLetters = selectedLetters.size();
	SortWord(0, sizeOfSelectedLetters - 1);
	for(int i = 0; i < sizeOfSelectedLetters - 1; ++i)
	{
		for(int j = 1; j < sizeOfSelectedLetters; ++j)
		{
			if(selectedLetters[j].arrIndex < selectedLetters[i].arrIndex)
			{
				SelectedLetter tempLetter = selectedLetters[i];
				selectedLetters[i] = selectedLetters[j];
				selectedLetters[j] = tempLetter;
			}
		}
	}

	for(int i = 0; i < sizeOfSelectedLetters; ++i)
	{
		int currentIndex = selectedLetters[i].arrIndex;
		int indexAbove = selectedLetters[i].arrIndex - GameInfo::Columns;
		while(currentIndex > -1)
		{
			RemoveLetter(indexAbove, currentIndex);
		}
	}
}

void MainGame::RemoveLetter(int& _indexAbove, int& _currentIdex)
{
	//TOP ROW
	if(_currentIdex < GameInfo::Columns)
	{
		board[_currentIdex] = '/';
		_currentIdex -= GameInfo::Columns;
	}
	else
	{
		//BLANK ABOVE
		if(board[_indexAbove] == '/')
		{
			board[_currentIdex] = '/';
			_currentIdex = -1;
		}
		else
		{
			board[_currentIdex] = board[_indexAbove];
			_currentIdex -= GameInfo::Columns;
			_indexAbove -= GameInfo::Columns;
		}
	}
}

bool MainGame::AddLetterToColumn(char _letter, int _column)
{
	int currentIndex = _column;
	char currentTile = board[currentIndex];
	
	//OK TO PLACE HERE
	if(currentTile == '/')
	{
		board[currentIndex] = _letter;

		while(DropLetter(currentIndex)){}

		return true;
	}
	//CANNOT PLACE HERE
	else
	{
		return false;
	}
}

bool MainGame::DropLetter(int& _currentIndex)
{
	if(board[_currentIndex + GameInfo::Columns] == '/')
	{
		board[_currentIndex + GameInfo::Columns] = board[_currentIndex];
		board[_currentIndex] = '/';
		_currentIndex += GameInfo::Columns;
		return true;
	}
	else
	{
		return false;
	}
}

void MainGame::InitializeGrabBag()
{
	//E
	for(int i = 0; i < 12; ++i)
	{
		lettersGrabBag.push_back('E');
	}

	//A
	for(int i = 0; i < 9; ++i)
	{
		lettersGrabBag.push_back('A');
	}

	//I
	for(int i = 0; i < 9; ++i)
	{
		lettersGrabBag.push_back('I');
	}

	//O
	for(int i = 0; i < 8; ++i)
	{
		lettersGrabBag.push_back('O');
	}

	//N
	for(int i = 0; i < 6; ++i)
	{
		lettersGrabBag.push_back('N');
	}

	//R
	for(int i = 0; i < 6; ++i)
	{
		lettersGrabBag.push_back('R');
	}

	//T
	for(int i = 0; i < 6; ++i)
	{
		lettersGrabBag.push_back('T');
	}

	//L
	for(int i = 0; i < 4; ++i)
	{
		lettersGrabBag.push_back('L');
	}

	//S
	for(int i = 0; i < 4; ++i)
	{
		lettersGrabBag.push_back('S');
	}

	//U
	for(int i = 0; i < 4; ++i)
	{
		lettersGrabBag.push_back('U');
	}

	//D
	for(int i = 0; i < 4; ++i)
	{
		lettersGrabBag.push_back('D');
	}

	//G
	for(int i = 0; i < 3; ++i)
	{
		lettersGrabBag.push_back('G');
	}

	//B
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('B');
	}

	//C
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('C');
	}

	//M
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('M');
	}

	//P
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('P');
	}

	//F
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('F');
	}

	//H
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('H');
	}

	//V
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('V');
	}

	//W
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('W');
	}

	//Y
	for(int i = 0; i < 2; ++i)
	{
		lettersGrabBag.push_back('Y');
	}

	//K
	for(int i = 0; i < 1; ++i)
	{
		lettersGrabBag.push_back('K');
	}

	//J
	for(int i = 0; i < 1; ++i)
	{
		lettersGrabBag.push_back('J');
	}

	//X
	for(int i = 0; i < 1; ++i)
	{
		lettersGrabBag.push_back('X');
	}

	//Q
	for(int i = 0; i < 1; ++i)
	{
		lettersGrabBag.push_back('Q');
	}

	//Z
	for(int i = 0; i < 1; ++i)
	{
		lettersGrabBag.push_back('Z');
	}
}

void MainGame::HandleKeyInput(Event* _event)
{
	InputEvent* inputEvent = (InputEvent*)_event->GetParam();

	short inputflag = inputEvent->GetInputFlags();
	if(inputEvent->GetInputFlags() & ENTER_EVENT)
	{
		MainGame::GetInstance()->CheckSelectedWord();
	}
}

void MainGame::SortWord(int _start, int _end)
{
	int i = _start;
	int k = _end;

	if(_end - _start >= 1)
	{
		int pivot = selectedLetters[_start].arrIndex;

		while(k > i)
		{
			while(selectedLetters[i].arrIndex <= pivot && i <= _end && k > i)
			{
				i++;
			}
			while(selectedLetters[k].arrIndex > pivot && k >= _start && k >= i)
			{
				k--;
			}
			if(k > i)
			{
				Swap(i, k);
			}
		}
		Swap(_start, k);

		SortWord(_start, k - 1);
		SortWord(k + 1, _end);
	}
	else
		return;
}

void MainGame::Swap(int _index1, int _index2)
{
	SelectedLetter temp = selectedLetters[_index1];
	selectedLetters[_index1] = selectedLetters[_index2];
	selectedLetters[_index2] = temp;
}

int MainGame::GetValueOfLetter(char _letter)
{
	if( _letter == 'A' ||
		_letter == 'E' ||
		_letter == 'I' ||
		_letter == 'O' ||
		_letter == 'U' ||
		_letter == 'L' ||
		_letter == 'N' ||
		_letter == 'R' ||
		_letter == 'S' ||
		_letter == 'T')
	{
		return 1;
	}
	else if(_letter == 'D' ||
			_letter == 'G')
	{
		return 2;
	}
	else if(_letter == 'B' ||
			_letter == 'C' ||
			_letter == 'M' ||
			_letter == 'P')
	{
		return 3;
	}
	else if(_letter == 'F' ||
			_letter == 'H' ||
			_letter == 'V' ||
			_letter == 'W' ||
			_letter == 'Y')
	{
		return 4;
	}
	else if(_letter == 'K')
	{
		return 5;
	}
	else if(_letter == 'J' ||
			_letter == 'X')
	{
		return 8;
	}
	else
		return 10;
}